<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>用户详情</title>
  <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
  <!-- <link rel="stylesheet" href="../../css/public.css" media="all"> -->
</head>

<body>
  <div class="layuimini-container">
    <div class="layuimini-main">

      <!-- <fieldset class="table-search-fieldset"> -->
      <div style="margin: 10px 10px 10px 10px">
        <div class="layui-form layui-form-pane" action="">
          <div class="layui-form-item">
            <div class="layui-form-item">
              <label class="layui-form-label">医生Id</label>
              <div class="layui-input-block">
                <input type="text" id="id" name="id" autocomplete="off" autocomplete="off"
                  class="layui-input" disabled>
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">医生姓名</label>
              <div class="layui-input-block">
                <input type="text" id="name" name="name" autocomplete="off" autocomplete="off"
                  class="layui-input" lay-verify="name|required">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">医生工号</label>
              <div class="layui-input-block">
                <input type="text" id="workNumber" name="workNumber" autocomplete="off" autocomplete="off" class="layui-input"
                  lay-verify="workNumber|required" >
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label" >性别</label>
              <div class="layui-input-inline" style="width: 30%;">
                  <select name="gender" id="gender">
                      <option value="true" >男</option>
                      <option value="false">女</option>
                  </select>
              </div>
            </div>
            <div class="layui-form-item" id = "passwordButton">
              <button id="resetPassword" type="button" class="layui-btn layui-btn-sm layui-btn-primary layui-border-green" style="margin-right: 0;" 
                lay-filter="saveBtn" onclick="resetPassword()">重置密码</button>
            </div>
            <div class="layui-form-item"  id="passwordBlock" style="visibility:hidden">
              <label class="layui-form-label">密码</label>
              <div class="layui-input-block">
                <input type="text" id="password" name="password" autocomplete="off" class="layui-input" 
                   placeholder="请输入正确格式6-20位由数字和字母组成的密码" lay-verify="password">
              </div>
            </div>
            <!-- <div class="layui-inline">
              <div class="layui-input-inline">
                <button class="layui-form-label" lay-submit lay-filter="saveBtn">确认修改</button>
              </div>
            </div> -->
            <div style="position: absolute; right: 17px;">
              <button id="closebtn" type="button" class="layui-btn" style="margin-right: 0;" lay-submit lay-filter="saveBtn">保存提交</button>
            </div>
          </div>
        </div>
      </div>
      <!-- </fieldset> -->
    </div>
  </div>

  <!-- <div style="position: absolute; right: 17px;">
        <button id="closebtn" type="button" class="layui-btn" style="margin-right: 0;">关闭</button>
    </div> -->
  <script src="../../lib/layui-v2.6.3/layui.js"></script>
  <script src="../../js/basic-load.js" charset="utf-8"></script>
  <script>
    var method = "新增" // 也可能是"修改"，由传参决定
    layui.use(['laydate', 'form', 'jquery', 'table'], function () {
      var laydate = layui.laydate,
        form = layui.form,
        table = layui.table,
        $ = layui.$;

      //执行一个laydate实例
      laydate.render({
        elem: '#publication_data' //指定元素
      });
      form.verify({
        name: function (value) {
          if (value.length > 10) {
            return '最大长度不超过10个字符';
          }
        }
        , workNumber: [
          /^(\d{11})?$/
          , '请输入正确的11位工号'
        ]
        , password:[
          /^([A-Za-z\d]{6,20})?$/
          , '请输入正确格式6-20位由数字和字母组成的密码'
        ]
        ,
      });

      //监听提交
      form.on('submit(saveBtn)', function (data) {
        // document.getElementById("userPassword").select();
        for (var key in data.field) {
          if(method == "修改" && key == "password" && data.field[key] === '')
            continue
          if (data.field[key] === '')
            delete data.field[key]
        }
        console.log(data.field)
        if(method == '新增'){
          delete data.field["id"]
        }
        console.log('修改用户信息，发送数据如下：')
        console.log(JSON.stringify(data.field))
        $.ajax({
          url: `${url_basic}/doctor/${method == '修改' ? 'update' : 'register'}`,
          type: "POST",
          data: JSON.stringify(data.field),
          contentType: 'application/json',
          success: function (data, textStatus) {
            console.log(data)
            console.log(textStatus)
            if (data.code == 0) {
              layer.msg('成功', { time: 0.5 * 1000 }, function () {
                // 关闭弹出层
                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
              });
              return true;
            }
            else {
              layer.msg('失败，原因：' + data.msg);
              return false;
            }
          },
          error: function (xhr, errorText, errorType) {
            console.log(xhr)
            console.log(xhr.status)
            console.log(errorType)
            layer.msg('修改失败（与服务器连接失败）');
          },
          complete: function () {
            console.log('---------')
          },
          dataType: "json"
        })
        return false;
      });
    });
    function setData(data, privilege,_method) {

      // document.getElementById('closebtn').style.display = 'inline'
      this_privilege = privilege
      console.log(data)
      if(_method == 1 ){
        method = '新增'
        document.getElementById('id').value = '注册后分配'
        document.getElementById('passwordBlock').style.visibility = "visible"
        document.getElementById('passwordButton').style.display = "none"
      }else{
        method = '修改'
        document.getElementById('name').value = data.name
        document.getElementById('id').value = data.id
        document.getElementById('workNumber').value = data.workNumber
        if(data.gender == false){
          var obj  = document.getElementById('gender')
          obj.innerHTML = "<option value=\"true\" >男</option> <option value=\"false\" selected>女</option>"
          console.log(obj)
          layui.form.render("select")
        }
      }

    };
    function resetPassword(){
      console.log("turn to visable")
      document.getElementById('passwordBlock').style.visibility = "visible"
    }
  </script>
</body>

</html>